package cn.yyh.serviceOrg.controller;

import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceOrg.entity.User;
import cn.yyh.serviceOrg.entity.dto.UserDTO;
import cn.yyh.serviceOrg.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: UserController
 * @Package: cn.yyh.user.controller
 * @date 2021/6/30
 */
@RestController
@RequestMapping("/serviceOrg/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户手机注册")
    @PostMapping("/phone/register")
    public R phoneRegister(@RequestBody UserDTO userDTO) {
        try {
            userService.phoneRegister(userDTO);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value = "编号",required = true)
            @PathVariable Long id) {

        try {
            User byId = userService.getById(id);
            return R.oK().data("rows", byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据有没有ID判断是添加还是修改")
    @PostMapping("save")
    public R saveOrUpdate(
            @ApiParam(name = "employee",value = "数据封装对象")
            @RequestBody User user) {

        try {
            if (user.getId() == null) {
                userService.save(user);
            }else {
                userService.update(user);
            }
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("{id}")
    public R delete(
            @ApiParam(name = "id",value = "编号",required = true)
            @PathVariable Long id){

        try {
            userService.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "根据id批量删除员工")
    @PatchMapping("/deleteBatch")
    public R deleteBatch(
            @ApiParam(name = "ids",value = "编号集合",required = true)
            @RequestBody List<Long> ids){

        try {
            userService.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("getByLoginInfoId/{id}")
    public R getByLoginInfoId(@PathVariable("id") Long id) {
        try {
            User user = userService.getByLoginInfoId(id);
            //String s = JSONObject.toJSONString(user);

            return R.oK().data("user",user);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
